/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.gob.fonafe.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import pe.gob.fonafe.domain.GaleriaFotosBean;
import pe.gob.fonafe.domain.InmuebleBean;
import pe.gob.fonafe.domain.MuebleBean;
/**
 *
 * @author Irving
 */
public class MuestraImagenMuebleGLC extends ActionSupport implements Preparable,RequestAware{
    
    
     private static final Logger logger = Logger.getLogger(MuestraImagenInmuble.class);
    private Map<String,Object> request;

    private String idMueble;
    private String idFotoGaleria;
    /*Params request*/
    private InputStream inputStream;
    private String mimeType;

    public void prepare() throws Exception {
        //inputStream =
        mimeType ="";
    }

    public String verImagenMuebleGLC(){
        String resultado="";
        try{
            HttpServletRequest req = ServletActionContext.getRequest();
            HttpSession sesion = ServletActionContext.getRequest().getSession();

            String strFoto = req.getParameter("idFoto");
            List<MuebleBean> listMueble = (List<MuebleBean>)sesion.getAttribute("listaMuebles");
            if(!strFoto.equalsIgnoreCase("")){
                byte[]btFoto= null;
                for(MuebleBean bean: listMueble){
                    if((bean.getId_mueble()+"").equalsIgnoreCase(strFoto)){
                        btFoto = bean.getFoto_principal();
                        break;
                    }
                }
                if(btFoto != null){
                    inputStream = new ByteArrayInputStream(btFoto);
                    mimeType = "image/gif";
                    resultado = SUCCESS;
                }
            }
        }catch (Exception ex){
            resultado = ERROR;
            ex.printStackTrace();
        }
        return resultado;
    }
            
            
     public String verGaleriaMueble(){
        String resultado="";
        try{
            HttpServletRequest req = ServletActionContext.getRequest();
            HttpSession sesion = ServletActionContext.getRequest().getSession();

            String strFoto = req.getParameter("idFoto");
            List<GaleriaFotosBean> listInmu = (List<GaleriaFotosBean>)sesion.getAttribute("listaGaleria");
            
            if(!strFoto.equalsIgnoreCase("")){
                byte[]btFoto= null;
                for(GaleriaFotosBean bean: listInmu){
                    if(bean.getIdFotoGaleria().equalsIgnoreCase(strFoto)){
                        btFoto = bean.getFoto();
                        break;
                    }
                }
                if(btFoto != null){
                    inputStream = new ByteArrayInputStream(btFoto);
                    mimeType = "image/gif";
                    resultado = SUCCESS;
                }
            }
        }catch (Exception ex){
            resultado = ERROR;
            ex.printStackTrace();
        }
        return resultado;
    }
     
     public String verFotoPrincipalMueble()
     {
       String resultado="";
        try{
            HttpSession sesion = ServletActionContext.getRequest().getSession();
            MuebleBean muebleBean = (MuebleBean)sesion.getAttribute("muebleBean");
            if(!idMueble.equalsIgnoreCase("")){
                if(muebleBean.getFoto_principal() != null){
                 inputStream = new ByteArrayInputStream(muebleBean.getFoto_principal());
                 mimeType = "image/gif";
                 resultado = SUCCESS;
                }
            }
        }catch(Exception ex){
            resultado = ERROR;
            ex.printStackTrace();
        }
        return resultado;
     }
     
             
    public String verPlanoMueble()
     {
       String resultado="";
        try{
            HttpSession sesion = ServletActionContext.getRequest().getSession();
            MuebleBean muebleBean = (MuebleBean)sesion.getAttribute("muebleBean");
            if(!idMueble.equalsIgnoreCase("")){
                if(muebleBean.getPlanoUbicacion() != null){
                 inputStream = new ByteArrayInputStream(muebleBean.getPlanoUbicacion());
                 mimeType = "image/gif";
                 resultado = SUCCESS;
                }
            }
        }catch(Exception ex){
            resultado = ERROR;
            ex.printStackTrace();
        }
        return resultado;
     }
    
             
    public void setRequest(Map<String, Object> request) {
        this.request = request;
    }
    
    
    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String getMimeType() {
        return mimeType;
    }

    public void setMimeType(String mimeType) {
        this.mimeType = mimeType;
    }

    
    

    public String getIdFotoGaleria() {
        return idFotoGaleria;
    }

    public void setIdFotoGaleria(String idFotoGaleria) {
        this.idFotoGaleria = idFotoGaleria;
    }

    /**
     * @return the idMueble
     */
    public String getIdMueble() {
        return idMueble;
    }

    /**
     * @param idMueble the idMueble to set
     */
    public void setIdMueble(String idMueble) {
        this.idMueble = idMueble;
    }
    
    
}
